package com.quanyan.stadiumScheduler.biz.impl;

import com.quanyan.common.Plugin.Page;
import com.quanyan.stadiumScheduler.biz.BizGameSystemDao;
import com.quanyan.stadiumScheduler.constants.GameConstants;
import com.quanyan.stadiumScheduler.entity.*;
import com.quanyan.stadiumScheduler.entity.vo.VDefinedGameSchedule;
import com.quanyan.stadiumScheduler.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 */
@Service
public class BizGameSystemDaoImpl implements BizGameSystemDao {

    @Autowired
    private TbGameRoundMapper tbGameRoundMapper;

    @Autowired
    private TbGameScheduleMapper tbGameScheduleMapper;

    @Autowired
    private TbGameEventTeamMapper tbGameEventTeamMapper;

    @Autowired
    private VStadiumUserDefinedMapper vStadiumUserDefinedMapper;

    @Autowired
    private TbGameClubTeamUserMapper tbGameClubTeamUserMapper;
    @Autowired
    private TbUserGameScheduleMapper tbUserGameScheduleMapper;

    @Override
    public List<TbGameRound> selectTbGameRoundByGameEventId(Integer gameEventId) {
        TbGameRoundExample example = new TbGameRoundExample();
        TbGameRoundExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(gameEventId);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        return tbGameRoundMapper.selectByExample(example);
    }

    @Override
    public List<TbGameSchedule> selectTbGameScheduleByGameRoundId(Integer gameRoundId, Integer pageNum, Integer pageSize) {
        TbGameScheduleExample example = new TbGameScheduleExample();
        TbGameScheduleExample.Criteria criteria = example.createCriteria();
        criteria.andGameRoundIdEqualTo(gameRoundId);
        if (pageNum != null && pageSize != null) {
            example.setPage(new Page(pageNum * pageSize, pageSize));
        }
        return tbGameScheduleMapper.selectByExample(example);
    }

    @Override
    public  List<TbGameEventTeam> selectTbGameEventTeamByGameEventId(Integer gameEventId, Byte isDelete) {
        TbGameEventTeamExample example = new TbGameEventTeamExample();
        TbGameEventTeamExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(gameEventId);
        if (null != isDelete) {
            criteria.andIsDeleteEqualTo(isDelete);
        }
        example.setOrderByClause("score desc,id asc");
        return tbGameEventTeamMapper.selectByExample(example);
    }

    @Override
    public List<TbGameClubTeamUser> queryTbGameClubTeamUserByClubId(Integer gameEventId, Integer clubId) {
        TbGameClubTeamUserExample example = new TbGameClubTeamUserExample();
        TbGameClubTeamUserExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(gameEventId);
        if (null!=clubId) {
            criteria.andClubIdEqualTo(clubId);
        }
        return tbGameClubTeamUserMapper.selectByExample(example);
    }

    @Override
    public List<VDefinedGameSchedule> selectVDefinedGameScheduleByGameRoundId(Integer gameRoundId, Integer pageNum, Integer pageSize) {
        if (pageNum == null || pageSize == null || pageNum == 0) {
            pageNum=0;
            pageSize = 100;
        }
        return vStadiumUserDefinedMapper.selectVDefinedGameScheduleList(gameRoundId, pageNum*pageSize, pageSize);
    }

    @Override
    public void insertSelectiveUserGameSchedule(TbUserGameSchedule userGameSchedule) {
        tbUserGameScheduleMapper.insertSelective(userGameSchedule);
    }

    @Override
    public void updateSelectiveUserGameSchedule(TbUserGameSchedule userGameSchedule, TbUserGameScheduleExample example) {
        tbUserGameScheduleMapper.updateByExampleSelective(userGameSchedule, example);
    }

    @Override
    public TbGameSchedule selectGameSchedule(Integer id) {
        return tbGameScheduleMapper.selectByPrimaryKey(id);
    }

}
